package com.gamota.mobi;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

import com.androidquery.AQuery;

import com.gamota.mobi.adapter.ErrorHandler;
import com.gamota.mobi.background.BackgroundTasks;
import com.gamota.mobi.event.Event.PostCommentSuccessEvent;
import com.gamota.mobi.event.Event.RequestFailEvent;
import com.gamota.mobi.event.Event.UserInfoSuccessEvent;
import com.gamota.mobi.network.ProgressDialogManager;
import com.gamota.mobi.util.Constants;
import com.gamota.mobi.util.Utils;

import de.greenrobot.event.EventBus;

public class ReviewActivity extends Activity implements OnClickListener{
	
	private BackgroundTasks bgTask;
	private AQuery aq;
	private String accessToken;
	private RatingBar ratingBar;
	private String appId;
	private EditText editComment;
	private ProgressDialog pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_review);
		appId = getIntent().getStringExtra(Constants.SLUG);
		ratingBar = (RatingBar) findViewById(R.id.rating);
		editComment = (EditText) findViewById(R.id.edit_comment);
		bgTask = new BackgroundTasks(this);
		aq = new AQuery(this);
		accessToken = Utils.getStaticAccessToken(this);
//		if(accessToken!="3de71f9e84a325bbfb86466c15b40b1e"){
			bgTask.getUserInfo(accessToken);
//		}
		ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
				// TODO Auto-generated method stub
				switch ((int)rating) {
				case 0:
					aq.id(R.id.text_rating).text(R.string.review_0star);
					break;
				case 1:
					aq.id(R.id.text_rating).text(R.string.review_1star);
					break;
				case 2:
					aq.id(R.id.text_rating).text(R.string.review_2star);
					break;
				case 3:
					aq.id(R.id.text_rating).text(R.string.review_3star);
					break;
				case 4:
					aq.id(R.id.text_rating).text(R.string.review_4star);
					break;
				case 5:
					aq.id(R.id.text_rating).text(R.string.review_5star);
					break;
				default:
					aq.id(R.id.text_rating).text(R.string.review_0star);
					break;
				}
			}
		});
		aq.id(R.id.btn_ok).clicked(this);
		aq.id(R.id.btn_cancel).clicked(this);
	}
	
	public void onEventMainThread(UserInfoSuccessEvent event){
		aq.id(R.id.user_avatar).image(event.getUser().getAvatarUr(), false, true);
		aq.id(R.id.user_name).text(event.getUser().getUserName());
	}
	
	public void onEventMainThread(PostCommentSuccessEvent event){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		Toast.makeText(this, R.string.comment_sent, Toast.LENGTH_SHORT).show();
		finish();
	}
	
	public void onEventMainThread(RequestFailEvent event){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		ErrorHandler.getInstance().setContext(this).handleError(event.getErrorCode());
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		EventBus.getDefault().unregister(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		UILApplication myApp=UILApplication.getInstance();
		Configuration config = myApp.getConfig();  
		getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
		EventBus.getDefault().register(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_ok:
			if(ratingBar.getRating() > 0 && !TextUtils.isEmpty(editComment.getText().toString())){
				pDialog = ProgressDialogManager.showProgressDialog(this, R.string.loading);
				accessToken = Utils.getStaticAccessToken(this);
				bgTask.sendComment(accessToken, appId, editComment.getText().toString());
				bgTask.rate(accessToken, appId, ratingBar.getRating());
				System.err.println("send rate " + ratingBar.getRating());
			} else {
				Toast.makeText(this, R.string.not_rate_yet, Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.btn_cancel:
			finish();
			break;
		}
	}
}
